home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / security / Watcher / read_hist.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-07-14  |  1.9 KB  |  78 lines

  1. /*
  2.    read_hist: open the file containing the results of our last run.  If
  3.    it is not there then we assume that this is a first run and set the
  4.    head of the list to NULL.  Otherwise, we read the results of the previous
  5.    run into a mess of a data structure for later use in comparisons.
  6.  
  7.    Assumed format of history file:
  8.  
  9.    command
  10.    \tkey
  11.    \t\tname type value
  12.  
  13.    with the following definitions:
  14.     command: pipeline that was executed
  15.     key: value of key on line
  16.     name: output field name
  17.     type: field type (same as defined in output format)
  18.     value: what was in the field.
  19.  
  20.    We create a linked list of linked lists of linked lists.  An improvement
  21.    would be to change to a tree of some sort to speed up searches.
  22.  
  23.    Kenneth Ingham
  24.  
  25.    Copyright (C) 1987 The University of New Mexico
  26. */
  27.  
  28. #include "defs.h"
  29. #include "y.tab.h"
  30. #include <sys/errno.h>
  31.  
  32. read_hist()
  33. {
  34.     extern struct old_cmd_st *chead;
  35.     extern int errno;
  36.     extern char *sys_errlist[];
  37.     extern char *histfilename;
  38.     FILE *hf;
  39.     char line[MAX_STR];
  40.     struct old_cmd_st *cp;
  41.     struct val_st *vp;
  42.     struct key_st *kp;
  43.     FILE *open_hf();
  44.  
  45.     if ((hf = open_hf()) == NULL)
  46.         return; /* errors are dealt with in open_hf */
  47.  
  48.     chead = NULL;  cp = NULL;  kp = NULL;
  49.     while (fgets(line, MAX_STR, hf) != NULL) {
  50.         line[strlen(line)-1] = '\0'; /* kill trailing newline */
  51.  
  52.         if (line[0] != '\t') /* command */
  53.             hist_cmd(line, &chead, &cp, &kp, &vp);
  54.         else if (line[0] == '\t' && line[1] != '\t') { /* key */
  55.             if (hist_key(line, &cp, &kp, &vp) == FAIL) {
  56.                 chead = NULL;
  57.                 return;
  58.             }
  59.         }
  60.         else if (line[0] == '\t' && line[1] == '\t') { /* values */
  61.             if (hist_value(line, &kp, &vp) == FAIL) {
  62.                 chead = NULL;
  63.                 return;
  64.             }
  65.         }
  66.         errno = 0;
  67.     }
  68.  
  69.     if (errno) {
  70.         fprintf(stderr, "Warning: error reading %s: %s\n",
  71.             histfilename, sys_errlist[errno]);
  72.         fprintf(stderr, "Ignoring history file.\n\n");
  73.         chead = NULL;
  74.     }
  75.  
  76.     (void) fclose(hf);
  77. }
  78.